package io.renren.modules.generator.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.utils.excel.ExcelUtils;
import io.renren.modules.generator.dao.SequenceDao;
import io.renren.modules.generator.entity.OrdersGroupEntity;
import io.renren.modules.generator.export.OrderGroupExcelDataCreaterImpl;
import io.renren.modules.generator.service.OrdersGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-12-17 14:24:47
 */
@RestController
@RequestMapping("generator/ordersgroup")
public class OrdersGroupController {
    @Autowired
    private OrdersGroupService ordersGroupService;

    @Autowired
    private SequenceDao sequenceDao;

    @Autowired
    private ExcelUtils excelUtils;
    @Autowired
    private OrderGroupExcelDataCreaterImpl orderGroupExcelDataCreater;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:ordersgroup:list")
    public R list(@RequestParam Map<String, Object> params){

        Query query = new Query(params);

        List<OrdersGroupEntity> list = ordersGroupService.queryPage(query);

        int total =ordersGroupService.queryPageTotal(params);

        PageUtils pageUtils = new PageUtils(list, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("page", pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
			OrdersGroupEntity ordersGroup = ordersGroupService.selectById(id);

        return R.ok().put("ordersGroup", ordersGroup);
    }

    /**
     * 保存,订单编组
     */
    @RequestMapping("/save/{flag}")
    @RequiresPermissions("generator:ordersgroup:save")
    public R save(@RequestBody OrdersGroupEntity ordersGroup,@PathVariable String flag){
		ordersGroupService.save(ordersGroup);
		if(flag.equals("0")){
            sequenceDao.getNextValue("order_group_sequence");
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:ordersgroup:update")
    public R update(@RequestBody OrdersGroupEntity ordersGroup){
			ordersGroupService.updateById(ordersGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("generator:ordersgroup:delete")
    public R delete(@PathVariable("id") int id){
        ordersGroupService.delete(id);
        return R.ok();
    }


    @GetMapping("export")
    @RequiresPermissions("generator:ordersgroup:export")
    public void exportDate(@RequestParam Map<String, Object> params, HttpServletRequest request,
                           HttpServletResponse response) throws Exception {
        String[] lableNames = {"订单组号","提货地","目的地","车型","提货日期","到货日期","总箱数","总重量","重量装载率","总体积","体积装载率","总板数","板数装载率","更新时间",
                "创建人","创建时间","状态","预留字段1","预留字段2","备注"};
        String[] columnNames={"orderGroupNumber","fromDtAreaNames","toDtAreaNames","vehicleName","fromDate","toDate","orderTotalQuantity","orderTotalWeight","orderTotalWeightRate",
                "orderTotalVolume","orderTotalVolumeRate","orderTotalPlatesNumber","orderTotalPlatesNumberRate","lastModifiedTime","createdBy","createdTime","status",
                "extendFirst","extendSecond","remark"};

        Object flag = params.get("flag");
        if(flag != null){
            String flag1 = (String) flag;
            if(flag1.equals("true")){
                lableNames =new String[] {"订单组号","提货地","目的地","车型","提货日期","到货日期","总箱数","总重量","重量装载率","总体积","体积装载率",
                        "总板数","板数装载率","更新时间",
                        "创建人","创建时间","订单组状态","订单组预留字段1","订单组预留字段2","订单组备注","订单号","订单状态","订单创建人","订单创建时间",
                        "提货时间","到货时间",
                        "提货地址代码","提货地址名称","提货省","提货市","提货区","提货详细地址","提货联系人","提货人手机","提货人电话","提货人邮箱",
                        "到货地址代码","到货地址名称","到货省","到货市","到货区","到货详细地址","到货联系人","到货人手机","到货人电话","到货人邮箱",
                        "订单总箱数","订单总体积","订单总重量","订单总板数","实际收货总数","实际发货总数","数量差异","订单备注","订单更新时间","订单预留字段1",
                        "订单预留字段2"};
                columnNames= new String[]{"orderGroupNumber","fromDtAreaNames","toDtAreaNames","vehicleName","fromDate","toDate","orderTotalQuantity",
                        "orderTotalWeight","orderTotalWeightRate", "orderTotalVolume","orderTotalVolumeRate","orderTotalPlatesNumber",
                        "orderTotalPlatesNumberRate","lastModifiedTime",  "createdBy","createdTime","status", "extendFirst","extendSecond","remark",
                        "orderNumber","orderStatus","orderCreatedBy","orderCreatedTime","orderFromDate","orderToDate",
                        "fromAddressCode","fromAddressName","fromProvince","fromCity","fromArea","fromDetailedAddress","fromLinkMan","fromLinkManPhone",
                        "fromLinkManTelement","fromLinkManEmail",
                        "toAddressCode","toAddressName","toProvince","toCity","toArea","toDetailedAddress","toLinkMan","toLinkManPhone","toLinkManTelement",
                        "toLinkManEmail",
                        "quantityTotal","volumeTotal","weightTotal","platesNumberTotal","actualReceiveQuantityTotal","actualSendQuantityTotal",
                        "quantityDifferenceTotal","orderRemark","orderLastModifiedTime","orderExtendFirst","orderExtendSecond"};
            }
        }

         excelUtils.export(response,"数据导出.xlsx",lableNames,columnNames,params,orderGroupExcelDataCreater);

    }



}
